/**
 * Copyright (c) 2002-2011 旭鸣信息
 * All rights reserved. 
 * File：GoodsBrandDaoImpl.java
 * History:
 *       May 11, 2012: Initially created, liuzhi.
 */
package com.xteam.ecsystem.dao.Impl;

import java.util.ArrayList;
import java.util.List;

import net.sf.json.JSONObject;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import com.xteam.ecsystem.dao.IGoodsBrandDao;
import com.xteam.ecsystem.entities.BaseGoodsBrand;
import com.xteam.ecsystem.entities.BaseGoodsInfo;
import com.xteam.ecsystem.utils.PageCenterStation;

/**
 * 商品品牌数据访问实现类
 * 
 * @author liuzhi
 *
 */
@Repository
public class GoodsBrandDaoImpl extends DDLPrefix implements IGoodsBrandDao {
	
	@SuppressWarnings("unchecked")
	@Autowired
	private BaseDaoImpl baseDao;

	@SuppressWarnings("unchecked")
	public void delObject(String ids) {
		Class clazz = null;
		try {
			clazz = Class.forName("com.xteam.ecsystem.entities.BaseGoodsBrand");
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		}
		
		List list = new ArrayList();
		String oId[] = ids.split(",");
		for (String id : oId) {
			Object obj = this.baseDao.get(clazz, id);
			if (obj != null) {
				list.add(obj);
			}
		}
		this.baseDao.deleteAll(list);
	}

	@SuppressWarnings("unchecked")
	public String getGoodsBrandList(int pageIndex, int pageSize, String bgbName,
			String bgbState) {
		JSONObject jo = new JSONObject();
		try {
			List list = new ArrayList();
			
			String hql = SQL_FROM + BaseGoodsBrand.REF + SQL_WHERE +" 1=1 ";
			String countHQL = SQL_SELECT + " count(*) " + SQL_FROM + BaseGoodsBrand.REF + SQL_WHERE +" 1=1 ";
			
			if (bgbName != null && bgbName.length() > 0
					&& !bgbName.equals("undefined")
					&& !bgbName.equals("null")) {
				hql += SQL_AND + " bgbName " + SQL_LIKE + " ? ";
				countHQL += SQL_AND + " bgbName " + SQL_LIKE + " ? ";
				list.add("%" + bgbName + "%");

			}
			
			if(bgbState != null && bgbState.length() > 0
					&& !bgbState.equals("undefined")
					&& !bgbState.equals("null")){
				if(!"-1".equals(bgbState)){
					hql += SQL_AND + " bgbState=? ";
					countHQL += SQL_AND + " bgbState=? ";
					list.add(Integer.parseInt(bgbState));
				}
			}

			PageCenterStation<?> p = (PageCenterStation<?>) this.baseDao.queryList(countHQL, hql, list
					.toArray(), pageIndex * pageSize, pageSize);
			
			jo = JSONObject.fromObject(p);

		} catch (Exception e) {
			e.printStackTrace();
		}
		
		return jo.toString();
	}

	public void saveOrUpdateObject(Object obj, String id) {
		try {
			if (id != null && !id.equals("") && id.length() > 0){
				this.baseDao.update(obj);
			}else {
				this.baseDao.save(obj);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	@SuppressWarnings("unchecked")
	public String getGoodsBrandInfoById(String id) {
		JSONObject jo = new JSONObject();
		try {
			List list = new ArrayList();
			
			String hql = SQL_FROM + BaseGoodsBrand.REF + SQL_WHERE +" 1=1 " + SQL_AND + " bgbId=?";
			list = this.baseDao.find(hql, new Object[]{id});
			
			jo = JSONObject.fromObject((BaseGoodsBrand)list.get(0));

		} catch (Exception e) {
			e.printStackTrace();
		}
		
		return jo.toString();
	}

	@SuppressWarnings("unchecked")
	public void updateGoodsBrandStateById(String id) {
		Class clazz = null;
		try {
			clazz = Class.forName("com.xteam.ecsystem.entities.BaseGoodsBrand");
			
			BaseGoodsBrand goodsBrand = (BaseGoodsBrand)this.baseDao.get(clazz, id);
			if(goodsBrand.getBgbState() == 1){
				goodsBrand.setBgbState(0);
			}else{
				goodsBrand.setBgbState(1);
			}
			
			this.baseDao.saveOrUpdate(goodsBrand);
			
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		}
	}
	
	@SuppressWarnings("unchecked")
	public void delGoodsBgbLogo(String id){
		Class clazz = null;
		try {
			clazz = Class.forName("com.xteam.ecsystem.entities.BaseGoodsBrand");
			
			BaseGoodsBrand goodsBrand = (BaseGoodsBrand)this.baseDao.get(clazz, id);
			goodsBrand.setBgbLogo(null);
			
			this.baseDao.saveOrUpdate(goodsBrand);
			
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		}
	}

	public void delGoodsBrandByIds(String bgbIds) {
		if(bgbIds!=null){
			String oId[] = bgbIds.split(",");
			for (String id : oId) {
				String hql = "";
				try {
					hql = SQL_DELETE + SQL_FROM + BaseGoodsBrand.REF + SQL_WHERE
							+ " bgbId = ? ";
					this.baseDao.executeUpdate(hql, new Object[] { id });
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		}
	}

	@SuppressWarnings("unchecked")
	public boolean getBgiByBgb(String bgbIds){
		boolean isUsed = false;
		List list = new ArrayList();
		String hql = "";
		
		try {
			String oId[] = bgbIds.split(",");
			for (String id : oId) {
				hql = SQL_FROM + BaseGoodsInfo.REF + SQL_WHERE + "bgiBrand = ?";
				list = this.baseDao.find(hql, new Object[]{id});
				
				if (list != null && !list.isEmpty()) {
					isUsed = true;
					break;
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		return isUsed;
	}
}
